package com.example.dante.a17376060_wanghaikang_finaltest.cn.example.main.cn.example.useraction;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.dante.a17376060_wanghaikang_finaltest.R;
import com.example.dante.a17376060_wanghaikang_finaltest.cn.example.main.MainActivity;
import com.example.dante.a17376060_wanghaikang_finaltest.cn.example.main.cn.example.domain.News;
import com.example.dante.a17376060_wanghaikang_finaltest.cn.example.main.cn.example.utils.HttpUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener{
    EditText username,userpassword;
    Button login;
    TextView login_to_register;
    String user_name,password;
    String responseData;
    List<Integer> userlist;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    for(Integer user:userlist) {
                        if(user==1){
                            Intent intent=new Intent(getApplicationContext(),MainActivity.class);
                            intent.putExtra("user_name",user_name);
                            startActivity(intent);
                            LoginActivity.this.finish();
                        }else {
                            Toast.makeText(getApplicationContext(),"用户名或密码错误",Toast.LENGTH_SHORT).show();
                        }
                    }
                    break;
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();

    }

    private void initView() {
        username=findViewById(R.id.login_username);
        userpassword=findViewById(R.id.login_password);
        login=findViewById(R.id.login);
        login_to_register=findViewById(R.id.login_to_register);
        login.setOnClickListener(this);
        login_to_register.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.login_to_register:
                Intent intent=new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
                break;
            case R.id.login:
                loginaction();
        }
    }

    private void loginaction() {
        user_name=username.getText().toString();
        password=userpassword.getText().toString();
        okhttp();
    }

    private void okhttp() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpUtil.sendOkHttpRequest("http://47.101.175.33:8080/AndroidDataFinalTest/user_loginAction.action?user_name="+user_name+"&&user_password="+password, new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {

                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        responseData=response.body().string();
                        parseJsonWithGsonObject(responseData);
                    }
                });
            }
        }).start();
    }

    private void parseJsonWithGsonObject(String responseData) {
        Gson gson = new Gson();
        userlist = gson.fromJson(responseData, new TypeToken<List<Integer>>() {
        }.getType());
        Message msg = new Message();
        msg.what = 1;
        handler.sendMessage(msg);
    }
}
